<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Objective-C," />





  <link rel="alternate" href="/atom.xml" title="我是一颗小小的石头" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="本篇主要写一下datepicker里缺少的年月显示控件，当我们用到日期控件的需求是只显示年月时，这时你会发现，iOS没有提供原生的年月显示控件，于是乎我们就动手自己造个轮子">
<meta name="keywords" content="Objective-C">
<meta property="og:type" content="article">
<meta property="og:title" content="自定义显示年月的datepicker">
<meta property="og:url" content="http://rocksky.me/2016/11/20/2016-11-20datepicker/index.html">
<meta property="og:site_name" content="我是一颗小小的石头">
<meta property="og:description" content="本篇主要写一下datepicker里缺少的年月显示控件，当我们用到日期控件的需求是只显示年月时，这时你会发现，iOS没有提供原生的年月显示控件，于是乎我们就动手自己造个轮子">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-05-24T08:51:50.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="自定义显示年月的datepicker">
<meta name="twitter:description" content="本篇主要写一下datepicker里缺少的年月显示控件，当我们用到日期控件的需求是只显示年月时，这时你会发现，iOS没有提供原生的年月显示控件，于是乎我们就动手自己造个轮子">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"always","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'B3Z9QGBKUY',
      apiKey: '3e128e47b1d944db2bc5aea5bd932897',
      indexName: 'rockblog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"请输入关键字","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://rocksky.me/2016/11/20/2016-11-20datepicker/"/>





  <title>自定义显示年月的datepicker | 我是一颗小小的石头</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    <script>
	(function(){
		if(''){
			if (prompt('请输入小石头给你的密码，没有的话找小石头要','') !== ''){
				alert('恭喜你答错啦！');
				history.back();
			}
		}
	})();
    </script>

    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我是一颗小小的石头</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://rocksky.me/2016/11/20/2016-11-20datepicker/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="SKY">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我是一颗小小的石头">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">自定义显示年月的datepicker</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-11-20T08:45:22+08:00">
                2016-11-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Objective-C/" itemprop="url" rel="index">
                    <span itemprop="name">Objective-C</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a class="cloud-tie-join-count" href="/2016/11/20/2016-11-20datepicker/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count join-count" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2016/11/20/2016-11-20datepicker/" class="leancloud_visitors" data-flag-title="自定义显示年月的datepicker">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本篇主要写一下datepicker里缺少的年月显示控件，当我们用到日期控件的需求是只显示年月时，这时你会发现，iOS没有提供原生的年月显示控件，于是乎我们就动手自己造个轮子<br><a id="more"></a></p>
<ul>
<li>自定义的datepicker显示年月，为了与原生控件效果相近，我们采用pickerview来做。</li>
<li>首先定义.h文件</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">//时间控件类型</div><div class="line">typedef NS_ENUM(NSInteger,SKYDatePickerMode) &#123;</div><div class="line">    SKYDatePickerModeTime,  //</div><div class="line">    SKYDatePickerModeDate, //</div><div class="line">    SKYDatePickerModeDateAndTime, //</div><div class="line">    SKYDatePickerModeYearAndMonth //</div><div class="line">&#125;;  </div><div class="line"></div><div class="line">//代理方法设置</div><div class="line">@class SKYDatePicker;</div><div class="line">@protocol SKYDatePickerDelegate&lt;NSObject&gt;</div><div class="line">@optional</div><div class="line">- (void)sky_datePicker:(SKYDatePicker *)dataPicker resultDate:(NSString *)date;</div><div class="line">@end</div><div class="line"></div><div class="line">#define SKYPickerViewHeight         240</div><div class="line">#define SKYPickerToolbarHeight      44</div><div class="line">#define SKYPickerRowHeight          35</div><div class="line">#define SKYPickerToolbarColor       RGBA(255, 255, 255, 1)</div><div class="line">#define SKYPickerTitleColor         RGBA(77, 171, 252, 1)</div><div class="line">#define SKYPickerBackColor          RGBA(235, 235, 235, 1)</div><div class="line"></div><div class="line">@interface SKYDatePicker : UIControl</div><div class="line">@property (nonatomic, strong) NSDate *minDate;</div><div class="line">@property (nonatomic, strong) NSDate *maxDate;</div><div class="line">@property (nonatomic,   weak) id&lt;SKYDatePickerDelegate&gt; delegate;</div><div class="line"></div><div class="line">- (SKYDatePicker *)initWithDate:(NSDate *)date mode:(SKYDatePickerMode)mode;</div><div class="line">- (void)show;</div><div class="line">@end</div></pre></td></tr></table></figure>
<ul>
<li>接下来定义.m文件</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div><div class="line">341</div><div class="line">342</div><div class="line">343</div><div class="line">344</div><div class="line">345</div><div class="line">346</div><div class="line">347</div><div class="line">348</div><div class="line">349</div><div class="line">350</div><div class="line">351</div><div class="line">352</div><div class="line">353</div><div class="line">354</div><div class="line">355</div><div class="line">356</div><div class="line">357</div><div class="line">358</div><div class="line">359</div><div class="line">360</div><div class="line">361</div><div class="line">362</div><div class="line">363</div><div class="line">364</div><div class="line">365</div><div class="line">366</div></pre></td><td class="code"><pre><div class="line">@interface SKYDatePicker ()&lt;UIPickerViewDelegate,UIPickerViewDataSource,UIGestureRecognizerDelegate&gt;</div><div class="line">@property (nonatomic, strong) UIView         *coverView;</div><div class="line">@property (nonatomic, strong) UIView         *pickerBackView;</div><div class="line">@property (nonatomic, strong) UIView         *toolbarView;</div><div class="line">@property (nonatomic, strong) UIButton       *doneButton;</div><div class="line">@property (nonatomic, strong) UIButton       *cancleButton;</div><div class="line">@property (nonatomic, strong) UIPickerView *pickerView;</div><div class="line">@property (nonatomic, strong) UIDatePicker *datePicker;</div><div class="line">@property (nonatomic, strong) NSDate         *date;//控件初始时间</div><div class="line">@property (nonatomic, assign) SKYDatePickerMode mode;</div><div class="line">@property (nonatomic, strong) NSMutableArray *yearArray;</div><div class="line">@property (nonatomic, strong) NSMutableArray *monthArray;</div><div class="line">@property (nonatomic, strong) NSString *selectYear;//记录所选年份</div><div class="line">@property (nonatomic, strong) NSString *selectMonth;//记录所选月份</div><div class="line">@end</div><div class="line">@implementation SKYDatePicker</div><div class="line"></div><div class="line"></div><div class="line">#pragma mark -- 初始化时间控件</div><div class="line">- (SKYDatePicker *)initWithDate:(NSDate *)date mode:(SKYDatePickerMode)mode &#123;</div><div class="line">    if (self = [super init]) &#123;</div><div class="line">        //设置日期数据</div><div class="line">        self.date   = date;</div><div class="line">        self.mode = mode;</div><div class="line">        </div><div class="line">        //添加UI控件</div><div class="line">        [self setupUIControlMethodWithMode:mode];</div><div class="line">        </div><div class="line">        //配置原生控件参数</div><div class="line">        if (self.mode != SKYDatePickerModeYearAndMonth) &#123;</div><div class="line">            if (self.mode == SKYDatePickerModeDate) &#123;</div><div class="line">                self.datePicker.datePickerMode = UIDatePickerModeDate;</div><div class="line">            &#125;else if (self.mode == SKYDatePickerModeTime) &#123;</div><div class="line">                self.datePicker.datePickerMode = UIDatePickerModeTime;</div><div class="line">            &#125;else if (self.mode == SKYDatePickerModeDateAndTime) &#123;</div><div class="line">                self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        //添加点击手势</div><div class="line">        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];</div><div class="line">        tap.delegate = self;</div><div class="line">        [self addGestureRecognizer:tap];</div><div class="line">    &#125;</div><div class="line">    return self;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">#pragma mark -- 点击确定/取消按钮</div><div class="line">//确定</div><div class="line">- (void)doneButtonClickedMethod &#123;</div><div class="line">    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器</div><div class="line">    dateFormatter.dateFormat=@&quot;yyyy-MM-dd hh:mm:ss&quot;;//指定转date得日期格式化形式</div><div class="line">    </div><div class="line">    NSString *date = nil;</div><div class="line">    if (self.mode == SKYDatePickerModeYearAndMonth) &#123;</div><div class="line">        date = [NSString stringWithFormat:@&quot;%@-%@&quot;,self.selectYear,self.selectMonth];</div><div class="line">    &#125;else &#123;</div><div class="line">        date = [dateFormatter stringFromDate:self.datePicker.date];</div><div class="line">    &#125;</div><div class="line">    if (self.delegate &amp;&amp; [self.delegate respondsToSelector:@selector(sky_datePicker:resultDate:)]) &#123;</div><div class="line">        [self.delegate sky_datePicker:self resultDate:date];</div><div class="line">    &#125;</div><div class="line">    [self remove];</div><div class="line">&#125;</div><div class="line"></div><div class="line">//取消</div><div class="line">- (void)cancleButtonClickedMethod &#123;</div><div class="line">    [self remove];</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">#pragma mark -- 展示/取消时间控件</div><div class="line">- (void)show&#123;</div><div class="line">    //刷新控件数据</div><div class="line">    [self refreshDatePickerTimeMethod];</div><div class="line">    </div><div class="line">    //弹框动画</div><div class="line">    typeof(self) __weak weakSelf = self;</div><div class="line">    [UIView animateWithDuration:0.3 animations:^&#123;</div><div class="line">        weakSelf.coverView.alpha = 0.3;</div><div class="line">        weakSelf.pickerBackView.frame = CGRectMake(0, weakSelf.height - SKYPickerViewHeight, self.width, SKYPickerViewHeight);</div><div class="line">    &#125; completion:^(BOOL finished) &#123;</div><div class="line">        </div><div class="line">    &#125;];</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (void)remove &#123;</div><div class="line">    typeof(self) __weak weakSelf = self;</div><div class="line">    [UIView animateWithDuration:0.3 animations:^&#123;</div><div class="line">        weakSelf.coverView.alpha = 0;</div><div class="line">        weakSelf.pickerBackView.frame = CGRectMake(0, weakSelf.height, self.width, SKYPickerViewHeight);</div><div class="line">    &#125; completion:^(BOOL finished) &#123;</div><div class="line">        [weakSelf removeFromSuperview];</div><div class="line">    &#125;];</div><div class="line">&#125;</div><div class="line"></div><div class="line">//设置只有点击灰色蒙版才remove</div><div class="line">-(void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event&#123;</div><div class="line">    CGPoint point = [[touches anyObject] locationInView:self];</div><div class="line">    if (!CGRectContainsPoint(self.pickerBackView.frame, point)) &#123;</div><div class="line">        [self remove];</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">#pragma mark -- 刷新控件时间</div><div class="line">- (void)refreshDatePickerTimeMethod &#123;</div><div class="line">    //日期相关判断</div><div class="line">    if (!self.date) &#123; self.date = [NSDate date]; &#125;</div><div class="line">    if (!self.minDate)  &#123; self.minDate = [NSDate sky_getNewDateFromDate:self.date afterYears:-1000]; &#125;</div><div class="line">    if (!self.maxDate) &#123; self.maxDate = [NSDate sky_getNewDateFromDate:self.date afterYears:500]; &#125;</div><div class="line">    if ([self.date compare:self.minDate] == NSOrderedAscending) &#123;</div><div class="line">        [SKYControlTool sky_showAlertWithController:nil Message:@&quot;控件设置的初始日期不能小于最小日期&quot; doneTitle:@&quot;好的&quot; doneBlock:nil];</div><div class="line">        return;</div><div class="line">    &#125;</div><div class="line">    else if ([self.date compare:self.maxDate] == NSOrderedDescending) &#123;</div><div class="line">        [SKYControlTool sky_showAlertWithController:nil Message:@&quot;控件设置的最大日期不能小于初始日期&quot; doneTitle:@&quot;好的&quot; doneBlock:nil];</div><div class="line">        return;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    //</div><div class="line">    if (self.mode == SKYDatePickerModeYearAndMonth) &#123;//自定义控件</div><div class="line">        NSCalendar *calendar = [NSCalendar currentCalendar];</div><div class="line">        NSDateComponents *minCompent = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth</div><div class="line">                                                   fromDate:self.minDate];</div><div class="line">        NSDateComponents *maxCompent = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth</div><div class="line">                                                   fromDate:self.maxDate];</div><div class="line">        </div><div class="line">        //添加年份</div><div class="line">        [self.yearArray removeAllObjects];//清空</div><div class="line">        for (NSInteger year = minCompent.year; year &lt;= maxCompent.year; year++) &#123;</div><div class="line">            [self.yearArray addObject: [NSString stringWithFormat: @&quot;%zd&quot;, year]];</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        //添加月份</div><div class="line">        [self.monthArray removeAllObjects];//清空</div><div class="line">        for (NSInteger month = 1; month &lt;= 12; month++) &#123;</div><div class="line">            [self.monthArray addObject: [NSString stringWithFormat: @&quot;%02zd&quot;, month]];</div><div class="line">        &#125;</div><div class="line">        [self.pickerView reloadAllComponents];//刷新数据源</div><div class="line">        </div><div class="line">        </div><div class="line">        //设置选中时间</div><div class="line">        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];</div><div class="line">        [formatter setDateFormat:@&quot;yyyy&quot;];</div><div class="line">        NSInteger currentYear = [[formatter stringFromDate:self.date] integerValue];</div><div class="line">        [formatter setDateFormat:@&quot;MM&quot;];</div><div class="line">        NSInteger currentMonth = [[formatter stringFromDate:self.date] integerValue];</div><div class="line">        </div><div class="line">        NSInteger yearIndex    = [self.yearArray indexOfObject:[NSString stringWithFormat:@&quot;%li&quot;,currentYear]];</div><div class="line">        NSInteger monthIndex = [self.monthArray indexOfObject:[NSString stringWithFormat:@&quot;%02li&quot;,currentMonth]];</div><div class="line">        [self.pickerView selectRow:yearIndex inComponent:0 animated:YES];</div><div class="line">        [self.pickerView selectRow:monthIndex inComponent:1 animated:YES];</div><div class="line">        self.selectYear   = self.yearArray[yearIndex];</div><div class="line">        self.selectMonth = self.monthArray[monthIndex];</div><div class="line">    &#125;</div><div class="line">    else&#123;//原生控件</div><div class="line">        self.datePicker.date = self.date;</div><div class="line">        self.datePicker.minimumDate = self.minDate;</div><div class="line">        self.datePicker.maximumDate = self.maxDate;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">#pragma mark -- 添加UI控件</div><div class="line">- (void)setupUIControlMethodWithMode:(SKYDatePickerMode)mode &#123;</div><div class="line">    self.frame = CGRectMake(0, 0, DeviceWidth, DeviceHeight-64);</div><div class="line">    self.backgroundColor = [UIColor clearColor];</div><div class="line">    </div><div class="line">    //底部蒙版</div><div class="line">    _coverView = [[UIView alloc]initWithFrame:self.frame];</div><div class="line">    _coverView.backgroundColor = [UIColor blackColor];</div><div class="line">    _coverView.alpha = 0;</div><div class="line">    [self addSubview:self.coverView];</div><div class="line">    </div><div class="line">    //时间控件底部视图</div><div class="line">    _pickerBackView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height, self.width, SKYPickerViewHeight)];</div><div class="line">    _pickerBackView.backgroundColor = [UIColor whiteColor];</div><div class="line">    [self addSubview:self.pickerBackView];</div><div class="line">    </div><div class="line">    //工具栏</div><div class="line">    _toolbarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.pickerBackView.width, SKYPickerToolbarHeight)];</div><div class="line">    _toolbarView.backgroundColor = SKYPickerToolbarColor;</div><div class="line">    [self.pickerBackView addSubview:self.toolbarView];</div><div class="line">    </div><div class="line">    //确定/取消按钮</div><div class="line">    _doneButton = [UIButton sky_buttonWithTarget:self action:@selector(doneButtonClickedMethod) fontSize:18 title:@&quot;确定&quot;];</div><div class="line">    _doneButton.frame = CGRectMake(self.toolbarView.width - 80, 0, 80, self.toolbarView.height);</div><div class="line">    [_doneButton setTitleColor:SKYPickerTitleColor forState:UIControlStateNormal];</div><div class="line">    [self.toolbarView addSubview:self.doneButton];</div><div class="line">    </div><div class="line">    _cancleButton = [UIButton sky_buttonWithTarget:self action:@selector(cancleButtonClickedMethod) fontSize:18 title:@&quot;取消&quot;];</div><div class="line">    _cancleButton.frame = CGRectMake(0, 0, 80, self.toolbarView.height);</div><div class="line">    [_cancleButton setTitleColor:SKYPickerTitleColor forState:UIControlStateNormal];</div><div class="line">    [self.toolbarView addSubview:self.cancleButton];</div><div class="line">    </div><div class="line">    </div><div class="line">    //pickerView</div><div class="line">    if (SKYDatePickerModeYearAndMonth == mode) &#123;</div><div class="line">        [self.pickerBackView addSubview:self.pickerView];</div><div class="line">    &#125;else&#123;</div><div class="line">        [self.pickerBackView addSubview:self.datePicker];</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">#pragma mark - UIPickerViewDelegate</div><div class="line">- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component &#123;</div><div class="line">    //限制最大最小date</div><div class="line">    NSCalendar *calendar = [NSCalendar currentCalendar];</div><div class="line">    NSDateComponents *minCompent = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self.minDate];</div><div class="line">    NSDateComponents *maxCompent = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self.maxDate];</div><div class="line">    NSInteger tempMonth = 0;</div><div class="line">    </div><div class="line">    //获取当前选中时间</div><div class="line">    if (component == 0) &#123;//年份</div><div class="line">        self.selectYear = self.yearArray[row];</div><div class="line">        </div><div class="line">        if ([self.selectYear integerValue] == maxCompent.year) &#123;</div><div class="line">            tempMonth = maxCompent.month;</div><div class="line">            if (tempMonth &lt; 12) &#123;</div><div class="line">                self.selectMonth = self.monthArray[tempMonth-1];</div><div class="line">                [self.pickerView selectRow:tempMonth-1 inComponent:1 animated:YES];</div><div class="line">            &#125;</div><div class="line">        &#125;else if ([self.selectYear integerValue] == minCompent.year) &#123;</div><div class="line">            tempMonth = minCompent.month;</div><div class="line">            if (tempMonth &lt; 12 &amp;&amp; self.selectMonth &lt; self.monthArray[tempMonth-1]) &#123;</div><div class="line">                self.selectMonth = self.monthArray[tempMonth-1];</div><div class="line">                [self.pickerView selectRow:tempMonth-1 inComponent:1 animated:YES];</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    else if (component == 1)&#123;//月份</div><div class="line">        self.selectMonth = self.monthArray[row];</div><div class="line">        </div><div class="line">        if ([self.selectYear integerValue] == maxCompent.year) &#123;</div><div class="line">            tempMonth = maxCompent.month;</div><div class="line">            if (row &gt; tempMonth - 1) &#123;</div><div class="line">                [self.pickerView selectRow:tempMonth-1 inComponent:1 animated:YES];</div><div class="line">                self.selectMonth = self.monthArray[tempMonth-1];</div><div class="line">            &#125;</div><div class="line">        &#125;else if ([self.selectYear integerValue] == minCompent.year) &#123;</div><div class="line">            tempMonth = minCompent.month;</div><div class="line">            if (row &lt; tempMonth - 1) &#123;</div><div class="line">                [self.pickerView selectRow:tempMonth-1 inComponent:1 animated:YES];</div><div class="line">                self.selectMonth = self.monthArray[tempMonth-1];</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component&#123;</div><div class="line">    if (component == 0) &#123;</div><div class="line">        return [NSString stringWithFormat: @&quot;%@年&quot;, self.yearArray[row]];</div><div class="line">    &#125;else &#123;</div><div class="line">        return[NSString stringWithFormat: @&quot;%@月&quot;, self.monthArray[row]];</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view &#123;</div><div class="line">    UILabel *label = [UILabel  new];</div><div class="line">    label.textAlignment = NSTextAlignmentCenter;</div><div class="line">    //    label.textColor = SKYPickerTitleColor;</div><div class="line">    [label setFont:[UIFont boldSystemFontOfSize:18]];</div><div class="line">    label.text = [self pickerView:pickerView titleForRow:row forComponent:component];</div><div class="line">    //    if (component == 0) &#123;</div><div class="line">    //        label.frame = CGRectMake(0, 0, self.width * 0.5, SKYPickerRowHeight);</div><div class="line">    //    &#125;</div><div class="line">    //    else if (component == 1) &#123;</div><div class="line">    //        label.frame = CGRectMake(self.width * 0.5, 0, self.width * 0.5, SKYPickerRowHeight);</div><div class="line">    //    &#125;</div><div class="line">    </div><div class="line">    //  设置横线的颜色，实现显示或者隐藏</div><div class="line">    ((UILabel *)[pickerView.subviews objectAtIndex:1]).backgroundColor = RGBA(150, 150, 150, 1);</div><div class="line">    ((UILabel *)[pickerView.subviews objectAtIndex:2]).backgroundColor = RGBA(150, 150, 150, 1);</div><div class="line">    </div><div class="line">    return label;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component &#123;</div><div class="line">    return self.width / 3;//为了平行显示/3</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">#pragma mark - UIPickerViewDataSource</div><div class="line">- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView &#123;</div><div class="line">    return 2;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component &#123;</div><div class="line">    return SKYPickerRowHeight;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component &#123;</div><div class="line">    if (component == 0) &#123;//年份</div><div class="line">        return self.yearArray.count;</div><div class="line">    &#125;else&#123;</div><div class="line">        return self.monthArray.count;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">#pragma mark -- setter/getter</div><div class="line">- (UIPickerView *)pickerView&#123;</div><div class="line">    if (!_pickerView) &#123;</div><div class="line">        _pickerView = [[UIPickerView alloc] initWithFrame: CGRectMake(0, CGRectGetMaxY(self.toolbarView.frame), self.pickerBackView.width, SKYPickerViewHeight - SKYPickerToolbarHeight)];</div><div class="line">        //        _pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;xr</div><div class="line">        _pickerView.dataSource = self;</div><div class="line">        _pickerView.delegate = self;</div><div class="line">        _pickerView.backgroundColor = SKYPickerBackColor;</div><div class="line">    &#125;</div><div class="line">    return _pickerView;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">- (UIDatePicker *)datePicker&#123;</div><div class="line">    if (!_datePicker) &#123;</div><div class="line">        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.toolbarView.frame), self.pickerBackView.width, SKYPickerViewHeight - SKYPickerToolbarHeight)];</div><div class="line">        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@&quot;zh_CN&quot;];//日期控件的地区</div><div class="line">        _datePicker.backgroundColor = SKYPickerBackColor;</div><div class="line">    &#125;</div><div class="line">    return _datePicker;</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (NSMutableArray *)yearArray&#123;</div><div class="line">    if (!_yearArray) &#123;</div><div class="line">        _yearArray = [NSMutableArray array];</div><div class="line">    &#125;</div><div class="line">    return _yearArray;</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (NSMutableArray *)monthArray&#123;</div><div class="line">    if (!_monthArray) &#123;</div><div class="line">        _monthArray = [NSMutableArray array];</div><div class="line">    &#125;</div><div class="line">    return _monthArray;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">- (void)setMinDate:(NSDate *)minDate&#123;</div><div class="line">    _minDate = minDate;</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (void)setMaxDate:(NSDate *)maxDate&#123;</div><div class="line">    _maxDate = maxDate;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">/*</div><div class="line">// Only override drawRect: if you perform custom drawing.</div><div class="line">// An empty implementation adversely affects performance during animation.</div><div class="line">- (void)drawRect:(CGRect)rect &#123;</div><div class="line">    // Drawing code</div><div class="line">&#125;</div><div class="line">*/</div><div class="line"></div><div class="line">@end</div></pre></td></tr></table></figure>
<ul>
<li>如果有想直接拿去用的可以点击<a href="https://github.com/skyrock90/datepicker" target="_blank" rel="external">这里</a></li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Objective-C/" rel="tag"># Objective-C</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/02/2016-10-02appstoreregect/" rel="next" title="AppStore被拒汇总">
                <i class="fa fa-chevron-left"></i> AppStore被拒汇总
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/12/10/2016-12-10gitsetting/" rel="prev" title="git版本控制的配置">
                git版本控制的配置 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.jpg"
               alt="SKY" />
          <p class="site-author-name" itemprop="name">SKY</p>
           
              <p class="site-description motion-element" itemprop="description">SKY | iOS | develop | 移动</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/skyrock90" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/3124928107?is_all=1" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2016 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SKY</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  
    
    <script>
      var cloudTieConfig = {
        url: document.location.href, 
        sourceId: "",
        productKey: "0e863791628e4ea384c811d1cb509425",
        target: "cloud-tie-wrapper"
      };
    </script>
    <script src="https://img1.ws.126.net/f2e/tie/yun/sdk/loader.js"></script>
  










  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.1"></script>



  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("uMSsGE1N7o7xR0ddmluiyocN-gzGzoHsz", "GQJnywX1H3dn7BuHJB41cmAu");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

</body>
</html>
